package com.siemens.industrial.web.mvc.controller.admin;

import com.siemens.industrial.model.admin.Device;
import com.siemens.industrial.model.admin.SystemConfig;
import com.siemens.industrial.utils.*;
import com.siemens.industrial.web.mvc.service.ExportDataFormatService;
import com.siemens.industrial.web.mvc.service.WriteLogs;
import com.siemens.industrial.web.mvc.service.admin.ChartsService;
import com.siemens.industrial.web.mvc.service.admin.DeviceService;
import com.siemens.industrial.web.mvc.service.admin.TenantService;
import com.siemens.industrial.web.mvc.service.partner.PartnerChartsService;
import com.siemens.industrial.web.mvc.service.partner.PartnerDeviceStatePeriodService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.*;

/**
 * Created by shuan on 2016/11/15.
 */
@Controller
public class ChartsController {
    @Resource
    ChartsService chartsService;

    @Resource
    TenantService tenantService;

    @Resource
    PartnerChartsService partnerChartsService;

    @Resource
    PartnerDeviceStatePeriodService partnerDeviceStatePeriodService;

    @Resource
    DeviceService deviceService;

    @Resource
    WriteLogs writeLogs;

    @Resource
    ExportDataFormatService exportDataFormatService;

    @RequestMapping(value = "/admin/dashboard", method = RequestMethod.GET)
    public String getDashboard() {
        return "admin/dashboard";
    }

    @RequestMapping(value = "/admin/chart/sameDeviceDiffReason", method = RequestMethod.GET)
    public String getSameDeviceDiffReason() {
        return "admin/charts/sameDeviceDiffReasonChart";
    }

    @RequestMapping(value = "/admin/chart/sameDeviceSameReasonChart", method = RequestMethod.GET)
    public String getSameDevicesSameReason() {
        return "admin/charts/sameDeviceSameReasonChart";
    }

    @RequestMapping(value = "/admin/chart/sameIdleReasonDiffDeviceChart", method = RequestMethod.GET)
    public String getSameIdleReasonDiffDevice() {
        return "admin/charts/sameIdleReasonDiffDeviceChart";
    }

    //获取某租户 某设备的状态统计
    @RequestMapping(value = "/admin/chart/deviceStateBarChart", method = RequestMethod.GET)
    public String getDeviceStateBarChart() {
        return "admin/charts/deviceStateBarChart";
    }

    @RequestMapping(value = "/admin/chart/deviceServiceChart", method = RequestMethod.GET)
    public String getDeviceServiceChart() {
        return "admin/charts/deviceServiceChart";
    }

    @RequestMapping(value = "/admin/chart/shiftDeviceServiceChart", method = RequestMethod.GET)
    public String getShiftDeviceServiceChart() {
        return "admin/charts/shiftDeviceServiceChart";
    }

    @RequestMapping(value = "/admin/chart/shiftIdleChart", method = RequestMethod.GET)
    public String getShiftIdle() {
        return "admin/charts/shiftIdlePieChart";
    }

    @RequestMapping(value = "/admin/chart/dayOrShiftStateChart", method = RequestMethod.GET)
    public String getDayOrShiftStateChart() {
        return "admin/charts/dayOrShiftStatePieChart";
    }

    //获取所有设备根据租户
    @RequestMapping(value = "/admin/getDevicesByTenantId", method = RequestMethod.GET)
    @ResponseBody
    public Map getDevicesByTenantId(@RequestParam(value = "tenantId") String tenantId) {
        List deviceList = chartsService.getDevicesByTenantId(tenantId);
        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(deviceList).build();
    }

    //查询所有机床oee或availability或performance或quality的top10
    @RequestMapping(value = "/api/v1/admin/getAllDeviceTop", method = RequestMethod.GET)
    @ResponseBody
    public Map getAllDeviceTop(@RequestParam(value = "day") String day) {
        Map map = chartsService.getAllDeviceTop(day);
        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(map).build();
    }

    //获取数据：同机床不同原因(折线)
    @RequestMapping(value = "/admin/api/v1/getSameDeviceDiffReason", method = RequestMethod.GET)
    @ResponseBody
    public Map getSameDeviceDiffReason(@RequestParam(value = "tenant") String tenantId,
                                       @RequestParam(value = "device") String device,
                                       @RequestParam(value = "startTime") String startTime,
                                       @RequestParam(value = "endTime") String endTime) {
        try {
            Map map = partnerChartsService.getSameDeviceDiffTypeTime(tenantId, device, startTime, endTime);
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(map).build();
        } catch (Exception e) {
            System.out.print(e);
            return RestResponse.create(GlobalResponseCode.ERROR_Exception_ParseException).build();
        }
    }

    //获取数据：同机床同原因折线图
    @RequestMapping(value = "/admin/api/v1/getSameDeviceSameReason", method = RequestMethod.GET)
    @ResponseBody
    public Map getSameDeviceSameTypeTime(@RequestParam(value = "tenant") String tenantId,
                                         @RequestParam(value = "device") String device,
                                         @RequestParam(value = "reason") String reason,
                                         @RequestParam(value = "reasonName") String reasonName,
                                         @RequestParam(value = "startTime") String startTime,
                                         @RequestParam(value = "endTime") String endTime) {
        try {
            Map map = partnerChartsService.getSameDeviceSameTypeTime(tenantId, device, reason, reasonName, startTime, endTime);
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(map).build();
        } catch (Exception e) {
            System.out.print(e);
            return RestResponse.create(GlobalResponseCode.ERROR_Exception_ParseException).build();
        }
    }

    //获取数据：同原因全部机床折线图
    @RequestMapping(value = "/admin/api/v1/getSameReasonDevices", method = RequestMethod.GET)
    @ResponseBody
    public Map getSameReasonDevices(@RequestParam(value = "tenant") String tenantId,
                                    @RequestParam(value = "reason") String reason,
                                    @RequestParam(value = "reasonName") String reasonName,
                                    @RequestParam(value = "startTime") String startTime,
                                    @RequestParam(value = "endTime") String endTime) {
        try {
            Map map = partnerChartsService.getSameReasonDevices(tenantId, reason, reasonName, startTime, endTime);
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(map).build();
        } catch (Exception e) {
            System.out.print(e);
            return RestResponse.create(GlobalResponseCode.SUCCESS).build();
        }
    }

    //oee 对比 一段时间(原 可选)
    @RequestMapping(value = "/admin/api/v1/serviceContrast", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getServiceList(@RequestBody Map<String, Object> requestParam) {
        try {
            List list = chartsService.serviceCompare((ArrayList<Map<String, Object>>) requestParam.get("tenantAndDevice"), requestParam.get("startTime").toString(), requestParam.get("endTime").toString());
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(list).build();
        } catch (Exception e) {
            System.out.print(e);
            return RestResponse.create(GlobalResponseCode.ERROR_Exception_ParseException).build();
        }
    }

    //oee 对比 一段时间(oee=availability)
    @RequestMapping(value = "/admin/api/v1/serviceContrastNew", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getServiceListNew(@RequestBody Map<String, Object> requestParam, HttpServletRequest request) {
        try {
            List list = chartsService.serviceCompareNew((ArrayList<Map<String, Object>>) requestParam.get("tenantAndDevice"), requestParam.get("startTime").toString(), requestParam.get("endTime").toString());
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(list).build();
        } catch (Exception e) {
            System.out.print(e);
            return RestResponse.create(GlobalResponseCode.ERROR_Exception_ParseException).build();
        }
    }

    //获取某班次 performance availability quality oee数据
    @RequestMapping(value = "/admin/product/shiftService", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getShiftDeviceService(@RequestBody Map<String, Object> requestParam) {
        try {
//            List<Map<String, Object>> list = chartsService.getServiceByShift((ArrayList<Map<String, Object>>) requestParam.get("tenantAndDevice"), requestParam.get("day").toString(), requestParam.get("type").toString());
            List<Map<String, Object>> list = chartsService.getShiftService((ArrayList<Map<String, Object>>) requestParam.get("tenantAndDevice"), requestParam.get("day").toString(), requestParam.get("type").toString());
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(list).build();
        } catch (Exception e) {
            System.out.print(e);
            return RestResponse.create(GlobalResponseCode.ERROR_Exception_ParseException).build();
        }
    }

    //班次 时间段
    @RequestMapping(value = "/admin/api/v1/periodsoftime/shift", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getPeriodstime(@RequestParam(value = "tenant") String tenantId,
                                              @RequestParam(value = "startTime") String startTime) {
        List<Map<String, Object>> periodsoftimes = partnerDeviceStatePeriodService.getTimePeriod(tenantId, startTime);

        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(periodsoftimes).build();
    }

    //24小时不同班次空闲原因总体分布
    @RequestMapping(value = "/admin/api/v1/dayShiftFreeReasonTotal", method = RequestMethod.GET)
    @ResponseBody
    public Map dayShiftFreeReasonTotal(@RequestParam(value = "tenant") String tenantId,
                                       @RequestParam(value = "startTime") String startTime,
                                       @RequestParam(value = "endTime") String endTime,
                                       @RequestParam(value = "day") String day,
                                       @RequestParam(value = "deviceId") String deviceId,
                                       @RequestParam(value = "deviceName") String deviceName) {
        try {
            Map<String, Object> map = partnerChartsService.getShiftDeviceFreeInfo(tenantId, day, startTime, endTime, deviceId);
//            ExportExcel.exportExcel((List<Map<String,Object>>) map.get("dataSource"));
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(map).build();
        } catch (Exception e) {
            System.out.print(e);
            return RestResponse.create(GlobalResponseCode.ERROR_Exception_ParseException).build();
        }
    }

    //某一班次或一天的状态
    @RequestMapping(value = "/admin/api/v1/periodsoftime/dayOrShiftState", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getDayOrShiftState(@RequestParam(value = "tenant") String tenantId,
                                                  @RequestParam(value = "deviceId") String deviceId,
                                                  @RequestParam(value = "deviceName") String deviceName,
                                                  @RequestParam(value = "day") String day,
                                                  @RequestParam(value = "startTime") String startTime,
                                                  @RequestParam(value = "endTime") String endTime) {
        try {
            List<Map<String, Object>> shiftStates = partnerDeviceStatePeriodService.getOneShiftState(tenantId, day, startTime, endTime, deviceId);

            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(shiftStates).build();
        } catch (ParseException e) {
            System.out.print(e);
            return RestResponse.create(GlobalResponseCode.ERROR_Exception_ParseException).build();
        }
    }

    @RequestMapping(value = "/admin/api/v1/getDevicesByTypeAndModel", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getDevicesByTypeAndModel(@RequestParam(value = "type") String type,
                                                        @RequestParam(value = "model") String model) {
        try {
            List list = chartsService.getDevicesByTypeAndModel(type, model);
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(list).build();
        } catch (Exception e) {
            System.out.print(e);
            return RestResponse.create(GlobalResponseCode.ERROR_Exception_ParseException).build();
        }
    }

    //获取某租户、某设备发生的全部idle原因
    @RequestMapping(value = "/admin/api/v1/getIdleReasonByDevice", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getIdleReasonByDevice(@RequestParam(value = "tenant") String tenantId,
                                                     @RequestParam(value = "deviceId") String deviceId) {
        try {
            List list = partnerChartsService.getIdleReasonByDevice(tenantId, deviceId);
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(list).build();
        } catch (Exception e) {
            System.out.print(e);
            return RestResponse.create(GlobalResponseCode.ERROR_Exception_ParseException).build();
        }
    }

    //获取某租户 发生的全部idle原因
    @RequestMapping(value = "/admin/api/v1/getIdleReasonByTenant", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getIdleReasonByTenant(@RequestParam(value = "tenant") String tenantId) {
        try {
            List list = partnerChartsService.getIdleReasons(tenantId);
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(list).build();
        } catch (Exception e) {
            System.out.print(e);
            return RestResponse.create(GlobalResponseCode.ERROR_Exception_ParseException).build();
        }
    }

    @RequestMapping(value = "/admin/api/v1/StateContrast", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> geStateList(@RequestBody Map<String, Object> requestParam) {
        try {
            Map<String, List<Map<String, Object>>> map = chartsService.stateBuild(requestParam);
//            ExportExcel.exportExcel((List<Map<String,Object>>)map.get("dataSource"),"bar");
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(map).build();
        } catch (Exception e) {
            System.out.print(e);
            return RestResponse.create(GlobalResponseCode.ERROR_Exception_ParseException).build();
        }
    }

    //2
    @RequestMapping(value = "/admin/api/v1/SameDeviceSameTypeTimeExport", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> SameDeviceSameTypeTimeExport(@RequestParam(value = "tenant") String tenantId,
                                                            @RequestParam(value = "device") String device,
                                                            @RequestParam(value = "reason") String reason,
                                                            @RequestParam(value = "reasonName") String reasonName,
                                                            @RequestParam(value = "startTime") String startTime,
                                                            @RequestParam(value = "endTime") String endTime) {
        try {
            Map map = partnerChartsService.getSameDeviceSameTypeTime(tenantId, device, reason, reasonName, startTime, endTime);
            Device sieDevice = deviceService.getDeviceById(map.get("device").toString());
            List<Map<String, Object>> sheets = (List<Map<String, Object>>) map.get("idleInfo");
            String name = ExportExcel.exportExcel(sheets, sieDevice.getDeviceUniqueId());
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(name).build();
        } catch (Exception e) {
            return RestResponse.create(GlobalResponseCode.ERROR_Exception_ParseException).build();
        }
    }

    //3
    @RequestMapping(value = "/admin/api/v1/sameReasonDevicesExport", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> SameReasonDevices(@RequestParam(value = "tenant") String tenantId,
                                                 @RequestParam(value = "reason") String reason,
                                                 @RequestParam(value = "reasonName") String reasonName,
                                                 @RequestParam(value = "startTime") String startTime,
                                                 @RequestParam(value = "endTime") String endTime) {
        try {
            Map map = partnerChartsService.getSameReasonDevices(tenantId, reason, reasonName, startTime, endTime);
            List<String> MTids = (List<String>) map.get("series");
            List<Map<String, Object>> datas = (List<Map<String, Object>>) map.get("idleList");
            List<Map<String, Object>> sheets = exportDataFormatService.dataFormatForSameReasonExport(MTids, datas);
            String name = ExportExcel.exportExcel(sheets, map.get("reason").toString());
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(name).build();
        } catch (Exception e) {
            return RestResponse.create(GlobalResponseCode.SUCCESS).build();
        }
    }

    //1
    @RequestMapping(value = "/admin/api/v1/sameDeviceDiffReasonExport", method = RequestMethod.GET)
    @ResponseBody
    public Map SameDeviceDiffReasonExport(@RequestParam(value = "tenant") String tenantId,
                                          @RequestParam(value = "device") String device,
                                          @RequestParam(value = "startTime") String startTime,
                                          @RequestParam(value = "endTime") String endTime) {
        try {
            Map map = partnerChartsService.getSameDeviceDiffTypeTime(tenantId, device, startTime, endTime);
            List<String> reasons = (List<String>) map.get("reasonList");
            List<Map<String, Object>> datas = (List<Map<String, Object>>) map.get("idleInfo");
            List<Map<String, Object>> sheets = exportDataFormatService.dataFormatForDiffReasonExport(reasons, datas);
            Device sieDevice = deviceService.getDeviceById(device);
            String name = ExportExcel.exportExcel(sheets, sieDevice.getDeviceUniqueId());
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(name).build();
        } catch (Exception e) {
            System.out.print(e);
            return RestResponse.create(GlobalResponseCode.ERROR_Exception_ParseException).build();
        }
    }
}
